﻿using BCCommon;
using BCData.OA.BusinessFlowPushRecord;
using BCDto.OA.BusinessFlowPushRecord;
using BCEntity.OA.BusinessFlowPushRecord;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Text;

namespace BCService.OA.BusinessFlowPushRecord
{
    public class BusinessFlowPushRecordService : IBusinessFlowPushRecordService
    {
        private readonly IBusinessFlowPushRecordData businessFlowPushRecordData;

        public BusinessFlowPushRecordService(IBusinessFlowPushRecordData businessFlowPushRecordData)
        {
            this.businessFlowPushRecordData = businessFlowPushRecordData;
        }

        public bool Exists(long businessFlowPushRecordId)
        {
            return this.businessFlowPushRecordData.Exists(businessFlowPushRecordId).GetAwaiter().GetResult();
        }
        public bool Exists(string businessFlowModelCode, string referenceNo)
        {
            return this.businessFlowPushRecordData.Exists(businessFlowModelCode.Trim(), referenceNo.Trim()).GetAwaiter().GetResult();
        }
        public BusinessFlowPushRecordDto Add(BusinessFlowPushRecordRequestDto requestDto)
        {

            var entity = requestDto.As<BusinessFlowPushRecordEntity>();

            return this.businessFlowPushRecordData.Add(entity).GetAwaiter().GetResult().As<BusinessFlowPushRecordDto>();
        }
        public BusinessFlowPushRecordDto Update(BusinessFlowPushRecordPutDto putDto)
        {
            if (!this.Exists(putDto.BusinessFlowPushRecordId))
            {
                throw new ArgumentException("此记录不存在");
            }
            var entity = putDto.As<BusinessFlowPushRecordEntity>();

            return this.businessFlowPushRecordData.Update(entity).GetAwaiter().GetResult().As<BusinessFlowPushRecordDto>();
        }

        public bool Delete(long businessFlowPushRecordId)
        {
            if (!this.Exists(businessFlowPushRecordId))
            {
                throw new ArgumentException("此记录不存在");
            }

            return this.businessFlowPushRecordData.Delete(businessFlowPushRecordId).GetAwaiter().GetResult();
        }

        public BusinessFlowPushRecordDto GetEntity(long businessFlowPushRecordId)
        {
            return this.businessFlowPushRecordData.GetEntity(businessFlowPushRecordId).GetAwaiter().GetResult().As<BusinessFlowPushRecordDto>();
        }

        public BusinessFlowPushRecordDto GetEntity(string businessFlowModelCode, string referenceNo)
        {
            return this.businessFlowPushRecordData.GetEntity(businessFlowModelCode, referenceNo).GetAwaiter().GetResult().As<BusinessFlowPushRecordDto>();
        }

        public PageableList<BusinessFlowPushRecordDto> Query(string businessFlowModelCode, string referenceNo, DateTime? start, DateTime? end, int pageIndex, int pageSize)
        {
            var (list, count) = this.businessFlowPushRecordData.Query(businessFlowModelCode, referenceNo, start, end, pageIndex, pageSize).GetAwaiter().GetResult();

            return new PageableList<BusinessFlowPushRecordDto>
            {
                Items = list.As<IEnumerable<BusinessFlowPushRecordDto>>(),
                Count = count,
                PageIndex = pageIndex,
                PageSize = pageSize
            };
        }
    }
}
